package com.mibao.activity;

import com.mipai.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
/**
 * 
 * @author xieyinchao
 * 主界面逻辑
 */
public class MainActivity extends Activity {
	public static final String MainActivity = "MainActivity";

	protected static final int SENSOR_SHAKE = 0;
	private SensorManager sensorManager;
	private Vibrator vibrator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);

        initView();


		try {
			sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
			vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

			final ComponentName activityName = new ComponentName(this, WizardActivity.class);
			final PackageManager pm = this.getPackageManager();
			int res = pm.getComponentEnabledSetting(activityName);

			if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT || res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
				pm.setComponentEnabledSetting(activityName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
			} else {
				
			}

		} catch (Exception ex) {}

	}

    private void initView(){
        OnClickListener onClickListener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.main_mipai:
                        startActivity(new Intent(MainActivity.this, CameraActivity.class));
                        break;
                    case R.id.main_miyou:
                        startActivity(new Intent(MainActivity.this, ContactsActivity.class));
                        break;
                    case R.id.main_xiangce:
                        startActivity(new Intent(MainActivity.this, GalleryActivity.class));  //XYC
                        break;
                    case R.id.main_yinsi:
                        startActivity(new Intent(MainActivity.this, HideAppActivity.class));
                        break;
                    case R.id.head_right_img: //设置
                    	startActivity(new Intent(MainActivity.this, SetPwdActivity.class));
                    default:
                        break;
                }
            }
        };
        findViewById(R.id.main_mipai).setOnClickListener(onClickListener);
        findViewById(R.id.main_miyou).setOnClickListener(onClickListener);
        findViewById(R.id.main_xiangce).setOnClickListener(onClickListener);
        findViewById(R.id.main_yinsi).setOnClickListener(onClickListener);
        findViewById(R.id.head_right_img).setOnClickListener(onClickListener);
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (sensorManager != null) {// 注册监听器
			sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
			// 第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率
		}
	}

	// 关闭的时候取摇一摇监听器
	@Override
	protected void onStop() {
		super.onStop();
		if (sensorManager != null) {// 取消监听器
			sensorManager.unregisterListener(sensorEventListener);
		}
	}

	/**
	 * 重力感应监听
	 */
	private SensorEventListener sensorEventListener = new SensorEventListener() {

		public void onSensorChanged(SensorEvent event) {
			// 传感器信息改变时执行该方法
			float[] values = event.values;
			float x = values[0]; // x轴方向的重力加速度，向右为正
			float y = values[1]; // y轴方向的重力加速度，向前为正
			float z = values[2]; // z轴方向的重力加速度，向上为正
			// Log.i(TAG, "x轴方向的重力加速度" + x + "；y轴方向的重力加速度" + y + "；z轴方向的重力加速度" +
			// z);
			// 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。
			int medumValue = 19;// 三星 i9250怎么晃都不会超过20，没办法只设置19
			if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
				vibrator.vibrate(200);
				Message msg = new Message();
				msg.what = SENSOR_SHAKE;
				handler.sendMessage(msg);
			}
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}
	};

	/**
	 * 动作执行
	 */
	Handler handler = new Handler() {

		@SuppressLint("HandlerLeak")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SENSOR_SHAKE:
				Finish();
				break;
			}
		}

	};

	private void Finish() {
		this.finish();
		System.exit(0);
	}

}
